/**
 * CS202 - Spring 2009 
 * Professor: Dr. Michael Peterson
 * Student: Deylo Woo (dwoo711@gmail.com)
 */
package battleship;

import java.util.Iterator;
import java.util.LinkedList;
/**
 * Custom Grid Class, that holds values on at multidimentional array
 * 
 */
public class Grid {

    private String[][] _grid;
    public Integer XSize = 0;
    public Integer YSize = 0;
/**
 * Constructor
 */
    public Grid() {
    }
/**
 * Constructor, taking in paramter for initialization
 * @param xSize: x grid size
 * @param ySize: y gird size
 */
    public Grid(Integer xSize, Integer ySize) {
        if (xSize > 0 && ySize > 0) {
            _grid = new String[xSize][ySize];
            XSize = xSize;
            YSize = ySize;
        } else {
            _grid = new String[1][1];
        }
        for (Integer i = 0; i < xSize; i++) {
            for (Integer j = 0; j < ySize; j++) {
                _grid[i][j] = "~";
            }
        }
    }

    /**
     * Assign value to a cell on Grid
     * @param xPosition
     * @param yPosition
     * @param value: value to assign
     */
    public void SetValue(Integer xPosition, Integer yPosition, String value) {
        _grid[xPosition][yPosition] = value;
    }
/**
 * Return value from a postion on a Grid
 * @param xPosition
 * @param yPosition
 * @return: return value form x y grid position
 */
    public String GetValue(Integer xPosition, Integer yPosition) {
        return _grid[xPosition][yPosition];
    }

    public void PrintGrid() {
        System.out.print("    ");
        for (Integer i = 0; i < XSize; i++) {
            System.out.print((i + 1) + "   ");
        }
        System.out.println();
        for (Integer i = 0; i < XSize; i++) {
            System.out.print((char) (i + 97));
            for (Integer j = 0; j < YSize; j++) {
                //_grid[i][j] = "~";
                System.out.print("   " + _grid[j][i]);
            }
            System.out.println();
        }
    }
/**
 * Print Grid for game
 * @param _ships: pass ships to match against grid to display proper values
 */
    public void PrintGrid4Game(LinkedList<Ship> _ships) {
        System.out.print("    ");
        for (Integer i = 0; i < XSize; i++) {
            System.out.print((i + 1) + "   ");
        }
        System.out.println();
        for (Integer i = 0; i < XSize; i++) {
            System.out.print((char) (i + 97));
            for (Integer j = 0; j < YSize; j++) {
                //_grid[i][j] = "~";
                if (_grid[j][i].equals(".") || _grid[j][i].equals("~")) {
                    System.out.print("   " + _grid[j][i]);
                } else if (Integer.parseInt(_grid[j][i]) >= 0) {
                    System.out.print("   " + GetPosValue(_ships, Integer.parseInt(_grid[j][i]), i, j));
                } else {
                    System.out.print("   " + "~");
                }
            }
            System.out.println();
        }
    }
/**
 * Get position value, passing ships, idx, x, y
 * @param _ships
 * @param idx: Ship index on Collection, also set on grid 
 * @param x
 * @param y
 * @return
 */
    private String GetPosValue(LinkedList<Ship> _ships, Integer idx, Integer x, Integer y) {
        Ship _ship = _ships.get(idx);
        Iterator _itr = _ship.Positions.iterator();
        while (_itr.hasNext()) {
            Position _pos = (Position) _itr.next();
            if (_pos.XValue == y && _pos.YValue == x) {
                if (_pos.HitPointValue.equals("1")) {
                    return "~";
                } else {
                    return _pos.HitPointValue;
                }
            }
        }
        return "n2";
    }
/**
 * Print for Game Over
 * @param _ships: passing ships to check on values against grid
 */
    public void PrintGridEndGame(LinkedList<Ship> _ships) {
        System.out.print("    ");
        for (Integer i = 0; i < XSize; i++) {
            System.out.print((i + 1) + "   ");
        }
        System.out.println();
        for (Integer i = 0; i < XSize; i++) {
            System.out.print((char) (i + 97));
            for (Integer j = 0; j < YSize; j++) {
                //_grid[i][j] = "~";
                if (_grid[j][i].equals(".") || _grid[j][i].equals("~")) {
                    System.out.print("   " + _grid[j][i]);
                } else if (Integer.parseInt(_grid[j][i]) >= 0) {
                    System.out.print("   " + GetPosValueEndGame(_ships, Integer.parseInt(_grid[j][i]), i, j));
                } else {
                    System.out.print("   " + "~");
                }
            }
            System.out.println();
        }
    }
/**
 * Return the value to the position to be printed on Grid
 * @param _ships
 * @param idx
 * @param x
 * @param y
 * @return
 */
    private String GetPosValueEndGame(LinkedList<Ship> _ships, Integer idx, Integer x, Integer y) {
        Ship _ship = _ships.get(idx);
        Iterator _itr = _ship.Positions.iterator();
        while (_itr.hasNext()) {
            Position _pos = (Position) _itr.next();
            if (_pos.XValue == y && _pos.YValue == x) {
                if (_pos.HitPointValue.equals("1")) {
                    return "O";
                } else {
                    return _pos.HitPointValue;
                }
            }
        }
        return "n2";
    }
/**
 * Get count of missed hist from grid
 * @return: the count of missed hits
 */
    public Integer GetMissHits() {
        Integer x = 0;
        for (Integer i = 0; i < XSize; i++) {
            for (Integer j = 0; j < YSize; j++) {
                if (_grid[j][i].equals(".")) {
                    x++;
                }
            }
        }
        return x;
    }
}
